<template>
  <div class="recommend-box">
    <h3>{{$t('wiki.detail.recommendBox.title1')}}</h3>
    <div class="recommend-list">
      <div
        :key="item.id"
        @click="toDetail(item)"
        v-for="item in recommendData"
        class="recommend-item">
        <h4 class="recommend-item-title" v-html="labelMark(item.title, 'title')"></h4>
        <div class="title-label" v-if="item.label && item.label.length > 0">
          <span
            :key="index"
            v-for="(tag, index) in item.label">
            {{ tag }}
          </span>
        </div>
        <div class="ql-container ql-snow content recommend-item-content" :class="fontClass" style="border:none">
          <div class="ql-editor">
            <div v-html="labelMark(item.content, 'content')" class="wiki-content"></div>
          </div>
        </div>
        <!-- <div class="recommend-item-content" v-html="labelMark(item.content)"></div> -->
      </div>
    </div>
  </div>
</template>

<script>

import { getOtherWiki } from '@src/api/Repository'
import { openTabForWikiView } from '@src/util/business/openTab'

export default {
  name: 'recommend-box',
  props: {
    wikiId: {

    },
    labels: {
      type: Array,
      default: () => []
    },
    fontClass: {

    }
    // recommendData: {
    //   type: Array,
    //   default: () => [{
    //     title: '你哈哈好你好， 发发发大V你的口你再见',
    //     content: 'ffsfvfdv你好，VN发大V发V哦降低搜发再见是发大V发是发是发v'
    //   }]
    // }
  },
  data() {
    return {
      recommendData: []
    }
  },
  mounted() {
    this.wikiId && this.getOtherWiki()
  },
  watch: {
    wikiId() {
      this.getOtherWiki()
    }
  },
  methods: {
    labelMark(text, type) {
      if (type === 'content') {
        let div = document.createElement('div')
        div.innerHTML = text
        text = div.innerText
      }
      let labels = this.labels
      if (!labels?.length) return type === 'content' ? `<p>${text}</p>` : text
      let reg = new RegExp(labels.join('|'), 'g')
      text = text.replaceAll(reg, '<span class="color-mark">$&</span>')
      return type === 'content' ? `<p>${text}</p>` : text
    },
    getOtherWiki() {
      getOtherWiki({
        wikiId: this.wikiId
      }).then(res => {
        if (res.code === 0) {
          res.result?.forEach(item => {
            if(!item.label) {
              item.label = []
              return
            }
            item.label = JSON.parse(item.label)
          })
          this.recommendData = res.result || []
        }
      })
    },
    toDetail(item) {
      openTabForWikiView(item.id)
    }
  },
}
</script>

<style lang="scss" scoped>
  ::v-deep .color-mark {
    color: $color-primary-light-6 !important;
  }
  .recommend-box {
    background: #fff;
    margin-top: 12px;
    border-radius: 4px;
    padding: 16px 100px;
    &>h3 {
      font-size: 16px;
      font-weight: bold;
      color: #262626;
      line-height: 22px;
      margin-bottom: 0;
    }
  }
  .recommend-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-top: 16px;
  }
  .recommend-item {
    border-bottom: 1px solid #E5E6E5;
    cursor: pointer;
    width: calc(50% - 8px);
    height: 171px;
    background: #FFFFFF;
    border-radius: 8px;
    border: 1px solid #D9D9D9;
    margin-bottom: 16px;
    padding: 24px;
    display: inline-flex;
    flex-direction: column;
    &-title {
      margin-bottom: 4px;
      height: 25px;
      font-size: 18px;
      // font-weight: bold;
      color: #262626;
      line-height: 25px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      flex-shrink: 0;
    }
    .title-label {
      margin-left: 0;
      min-height: 22px;
      display: inline-block;
      width: 100%;
      margin-bottom: 12px;
      overflow: hidden;
      span:first-child {
        margin-left: 0;
      }
    }
    &:hover {
      box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.12);
      .recommend-item-title {
        color: $color-primary-light-6;
      }
    }
    // &-content {
    //   font-size: 14px;
    //   font-weight: 400;
    //   color: #595959;
    //   line-height: 20px;
    // }
  }
  .ql-editor {
    overflow: hidden;
    .wiki-content {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
      cursor: pointer;
      font-size: 0;
      ::v-deep & >  * {
        font-size: 14px;
        font-weight: 400;
        color: #595959;
        line-height: 20px;
      }
      // ::v-deep p {
      //   font-size: 14px;
      //   font-weight: 400;
      //   color: #595959;
      //   line-height: 20px;
      // }
    }
  }
</style>